package com.xiaobao.coalproperty.module.find.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xiaobao.coalproperty.MainActivity;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.find.model.FindIndexEntity;
import com.xiaobao.coalproperty.module.find.model.ForumListEntity;
import com.xiaobao.coalproperty.other.adapter.TopicAdapter;
import com.xiaobao.coalproperty.other.base.BaseFragment;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

public class FindFragment extends BaseFragment<MainActivity> {

    @BindView(R.id.recycler)
    XRecyclerView mXRecyclerView;

    private View headView;
    private LinearLayout llTopic, llNews, llMoreTopic, llMoreNews;
    private FindIndexEntity mFindIndexEntity;
    private TopicAdapter mTopicAdapter;

    private ImageView ivNewsPic1, ivNewsPic2;
    private LinearLayout llNews1, llNews2;
    private TextView tvNewsTitle1, tvNewsTitle2, tvNewsContent1, tvNewsContent2, tvNewsTime1, tvNewsTime2;

    @Override
    protected int getContentView() {
        return R.layout.recycler;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        headView = LayoutInflater.from(getSuperActivity()).inflate(R.layout.fragment_find, null, false);
        llTopic = headView.findViewById(R.id.find_topic);
        llNews = headView.findViewById(R.id.find_news);
        llMoreTopic = headView.findViewById(R.id.find_more_topic);
        llMoreNews = headView.findViewById(R.id.find_more_news);
        ivNewsPic1 = headView.findViewById(R.id.find_news_iv1);
        ivNewsPic2 = headView.findViewById(R.id.find_news_iv2);
        llNews1 = headView.findViewById(R.id.find_news_ll1);
        llNews2 = headView.findViewById(R.id.find_news_ll2);
        tvNewsTitle1 = headView.findViewById(R.id.find_news_title1);
        tvNewsTitle2 = headView.findViewById(R.id.find_news_title2);
        tvNewsContent1 = headView.findViewById(R.id.find_news_content1);
        tvNewsContent2 = headView.findViewById(R.id.find_news_content2);
        tvNewsTime1 = headView.findViewById(R.id.find_news_time1);
        tvNewsTime2 = headView.findViewById(R.id.find_news_time2);

        mTopicAdapter = new TopicAdapter(getSuperActivity());
        mTopicAdapter.setOnItemClickListener(mOnItemClickListener);

        llTopic.setOnClickListener(mOnClickListener);
        llNews.setOnClickListener(mOnClickListener);
        llNews1.setOnClickListener(mOnClickListener);
        llNews2.setOnClickListener(mOnClickListener);
        llMoreTopic.setOnClickListener(mOnClickListener);
        llMoreNews.setOnClickListener(mOnClickListener);

        mXRecyclerView.setLayoutManager(new LinearLayoutManager(getSuperActivity(), LinearLayoutManager.VERTICAL, false));
        mXRecyclerView.addHeaderView(headView);
        mXRecyclerView.setLoadingListener(mLoadingListener);
        mXRecyclerView.setAdapter(mTopicAdapter);

        updateFindIndex();
    }

    private XRecyclerView.LoadingListener mLoadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            updateFindIndex();
            mXRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMore() {
            mXRecyclerView.loadMoreComplete();
        }
    };

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.find_topic:
                    startActivity(new Intent(getSuperActivity(), TopicActivity.class));
                    break;
                case R.id.find_news:
                    startActivity(new Intent(getSuperActivity(), NewsActivity.class));
                    break;
                case R.id.find_more_topic:
                    startActivity(new Intent(getSuperActivity(), TopicActivity.class));
                    break;
                case R.id.find_more_news:
                    startActivity(new Intent(getSuperActivity(), NewsActivity.class));
                    break;
                case R.id.find_news_ll1:
                    startNewsDetailActivity(0);
                    break;
                case R.id.find_news_ll2:
                    startNewsDetailActivity(1);
                    break;
            }
        }
    };


    private TopicAdapter.onItemClickListener mOnItemClickListener = new TopicAdapter.onItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getSuperActivity(), TopicDetailsActivity.class);
            intent.setAction("id");
            intent.putExtra("id", mTopicAdapter.getAdapterData().get(position).getId());
            startActivity(intent);
        }
    };

    private void startNewsDetailActivity(int index) {
        try {
            Intent intent = new Intent(getSuperActivity(), NewsDetailActivivty.class);
            intent.setAction("url");
            intent.putExtra("url", mFindIndexEntity.getData().getNews().get(index).getUrl());
            startActivity(intent);
        } catch (Exception e) {
        }
    }

    private void updateFindIndex() {
        OkHttpUtils.post().url(UrlConstant.URL_FIND_INDEX)
                .params(HttpHelperUtils.getInstance().findIndexParams())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    FindIndexEntity entity = new Gson().fromJson(response, FindIndexEntity.class);
                    if (entity.getCode() == 0) {
                        updateFindIndexSuccess(entity);
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }

    private void updateFindIndexSuccess(FindIndexEntity entity) {
        try {
            mFindIndexEntity = entity;
            Glide.with(getSuperActivity()).load(entity.getData().getNews().get(0).getPic()).into(ivNewsPic1);
            tvNewsTitle1.setText(entity.getData().getNews().get(0).getTitle());
            tvNewsContent1.setText(entity.getData().getNews().get(0).getContent());
            tvNewsTime1.setText(entity.getData().getNews().get(0).getDate());

            Glide.with(getSuperActivity()).load(entity.getData().getNews().get(1).getPic()).into(ivNewsPic2);
            tvNewsTitle2.setText(entity.getData().getNews().get(1).getTitle());
            tvNewsContent2.setText(entity.getData().getNews().get(1).getContent());
            tvNewsTime2.setText(entity.getData().getNews().get(1).getDate());

            List<ForumListEntity.DataBean.ListBean> mAdapterData = new ArrayList<>();
            for (int i = 0; i < entity.getData().getForum().size(); i++) {
                ForumListEntity.DataBean.ListBean bean = new ForumListEntity.DataBean.ListBean();
                bean.setId(entity.getData().getForum().get(i).getId());
                bean.setUid(entity.getData().getForum().get(i).getUid());
                bean.setAvatar(entity.getData().getForum().get(i).getAvatar());
                bean.setNickname(entity.getData().getForum().get(i).getNickname());
                bean.setTitle(entity.getData().getForum().get(i).getTitle());
                bean.setComment_num(entity.getData().getForum().get(i).getComment_num());
                bean.setDate(entity.getData().getForum().get(i).getDate());
                mAdapterData.add(bean);
            }
            mTopicAdapter.setAdapterData(mAdapterData);
        } catch (Exception e) {
        }
    }
}
